package com.bstek.urule.controller;

import com.bstek.urule.Utils;
import com.bstek.urule.enetiy.Customer;
import com.bstek.urule.runtime.*;
import com.bstek.urule.runtime.agenda.Activation;
import com.bstek.urule.runtime.agenda.AgendaFilter;
import com.bstek.urule.runtime.response.RuleExecutionResponse;
import com.bstek.urule.runtime.service.KnowledgeService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

@RestController
@RequestMapping("demo")
public class RuleController {

    private Logger log=Logger.getLogger(RuleController.class.getName());
	
	@RequestMapping("/rule")
    public RuleExecutionResponse rule(@RequestParam Integer data) throws IOException {
    	//创建一个KnowledgeSession对象
        KnowledgeService knowledgeService = (KnowledgeService) Utils.getApplicationContext().getBean(KnowledgeService.BEAN_ID);
        KnowledgePackage knowledgePackage = knowledgeService.getKnowledge("HCTest/1");//获取知识包，如果是第一次就远程调用，并存入到缓存中
        KnowledgeSession session = KnowledgeSessionFactory.newKnowledgeSession(knowledgePackage);
//        BatchSession batchSession = KnowledgeSessionFactory.newBatchSession(knowledgePackage);
//        batchSession.addBusiness();
//        param.put("com.bstek.urule.enetiy.Customer", customer);
//        List list = new ArrayList<>();
//        list.add(customer);
        Map<String, Object> param = new HashMap<>();
//        session.
        AgendaFilter agendaFilter = new AgendaFilter() {
            @Override
            public boolean accept(Activation activation) {
                return false;
            }
        };
        Customer customer2 = new Customer();
        Customer customer = new Customer();
        customer.setId(1L);
        customer.setAge(10);
        customer2.setId(2L);
        customer2.setAge(10);
        session.insert(customer);
        session.insert(customer2);

        int flag = 0;
        System.out.println(System.currentTimeMillis());
        for (int i = 0; i < 1; i++) {

            RuleExecutionResponse result = session.fireRules();
            session.insert(customer2);
            flag = i;
        }
        System.out.println(System.currentTimeMillis());
//        session.assertFact(customer2);
//        RuleExecutionResponse result = session.fireRules();

        System.out.println("年龄1：" + customer.getAge());
        System.out.println("年龄2：" + customer2.getAge());
        System.out.println("i的值：" + flag);
        return null;
    }

    @RequestMapping("/ageRule2")
    public RuleExecutionResponse rule2(@RequestParam Integer age) throws IOException {
        //创建一个KnowledgeSession对象
        KnowledgeService knowledgeService = (KnowledgeService) Utils.getApplicationContext().getBean(KnowledgeService.BEAN_ID);
        KnowledgePackage knowledgePackage = knowledgeService.getKnowledge("HCTest/2");
        KnowledgeSession session = KnowledgeSessionFactory.newKnowledgeSession(knowledgePackage);

        Map<String, Object> param = new HashMap<>();
        param.put("returnData", age);
        RuleExecutionResponse result = session.fireRules(param);

        System.out.println("returnData：" + session.getParameter("returnData"));
        return result;
    }

    @RequestMapping("/ageRule3")
    public RuleExecutionResponse rule3(@RequestParam Integer age, HttpServletRequest httpServletRequest)throws IOException{
        //从Spring中获取KnowledgeService接口实例
        KnowledgeService service=(KnowledgeService)Utils.getApplicationContext().getBean(KnowledgeService.BEAN_ID);
        //通过KnowledgeService接口获取指定的资源包"aaa"
        KnowledgePackage knowledgePackage=service.getKnowledge("HCTest/1");

        //通过取的KnowledgePackage对象创建BatchSession对象,在这个对象中，我们将开启5个线程，每个线程最多放置10个Bussiness接口实例运行
        BatchSession batchSession=KnowledgeSessionFactory.newBatchSession(knowledgePackage, 10, 100);
        int num = age;
        List<Customer> cusList = new ArrayList<>();
        long now = System.currentTimeMillis();
        log.info("+++++++++++++++++"+now);
        for(int i =0;i<num;i++){
            int finalI = i;
            batchSession.addBusiness(new Business(){
                @Override
                public void execute(KnowledgeSession session) {
//                    Customer employee=cusList.get(finalI);
                    Customer employee = new Customer();
                    employee.setAge(11);
                    //将业务数据对象Employee插入到KnowledgeSession中
                    session.insert(employee);
                    session.fireRules();
                    System.out.println(employee.getAge());
//                    session.startProcess("1");
                }
            });
        }
        //等待所有的线程执行完成，对于BatchSession调用来说，此行代码必不可少，否则将导致错误
        batchSession.waitForCompletion();
        log.info("-------------------"+(System.currentTimeMillis()-now));
        return null;
    }
}
